<?php

	namespace information\action;

	use herosphp\bean\Beans;
	use herosphp\core\Controller;
	use herosphp\core\WebApplication;
	use herosphp\http\HttpRequest;
	use herosphp\utils\Page;
	use herosphp\core\Loader;


	/**--tmpl-use--**/

	class IndexAction extends CommonAction {

		public function C_start()
		{
			$this->assign("index_url", url("/information/index/index"));
			$this->assign("more_url", url("/information/index/more"));
			$this->assign("detail_url", url("/information/index/detail"));
			parent::C_start();
		}

        public function index(HttpRequest $request)
		{
			$cates = $this->inforCateService->getItems(array(),array(),array('sort_index'=>1),9);
//			var_dump($cates);

			//每个栏目对应的资讯数量
			$limit = array(8,4,7,4,7,4,7,4,7);
			foreach($cates as $key=>$cate)
			{
				$information = null;
				$cates[$key]['information'] = $this->inforService->getItems(array('cate_id'=>$cate['id'],'release_status'=>1),array(),array(),$limit[$key]);
			}
			$adCount = intval((count($cates)-1)/2);
			$adslide = $this->adService->getItems(array('ad_place_key'=>'1416330bee3a41053a6907bfa430626d'),array(),array(),4); //走马灯广告

			$adrightkey = array("384e7ea480640d09a74921d0557ff972","d311f712184c17e5d68ec00063b5042e","7ba37249738476104045bf84cd271dc1");
			$adright = $this->adService->getItems(array('ad_place_key'=>array('$in'=>$adrightkey)),array(),array(),$adCount);//首页咨询右侧广告
			$admiddle = $this->adService->getItem(array('ad_place_key'=>'8bfb84b29392877961c58782d00b5a54'));//中部广告
			$adr = array();
			foreach($adright as $key=>$adtmp)
			{
				//资讯首页2，4，6对应的位置才有广告，所以将key值转化成相应的2,4,6
				$k = ($key+1)*2;
				$adr[$k] = $adtmp;
			}
			$this->assign('admiddle',$admiddle);
			$this->assign('advs',$adslide);
			$this->assign('adrights',$adr);
			$this->assign('items',$cates);
            $this->assign("title", "行业资讯");
            $this->setView(strtolower($request->getAction()) . '/info_index');

		}
		public function more(HttpRequest $request) {/*资讯更多*/
			$this->pagesize = 4;
			$page = $request->getParameter('page','intval');
			$page = $page>0?$page:1;
			$cate_id = $request->getParameter('cate','intval');
			$cate = $this->inforCateService->getItem(array('id'=>$cate_id));
			$items = $this->inforService->getItems(array('cate_id'=>$cate_id,'release_status'=>1),array(),array(),array($page,$this->pagesize));

			//如果分类不存在或者该分类没有咨询，则返回首页
			if(!$items){
				location(url("/information/index/index"));
			}

			parent::more($request);
			$recommend = $this->inforService->getItems(array('is_recommend'=>1,'cate_id'=>array('!='=>$cate_id)),array(),array('addtime'=>-1),10);

			$adslide = $this->adService->getItems(array('ad_place_key'=>'1416330bee3a41053a6907bfa430626d'),array(),array(),4); //走马灯广告
			$adtop = $this->adService->getItem(array('ad_place_key'=>"2e361db4c859a5d4f7bb75becf3d066c"));//顶部广告
			$admiddle = $this->adService->getItem(array('ad_place_key'=>"a588d85d1faf09fd0612ebca0774e42c"));//中部
			$this->assign('adtop',$adtop);
			$this->assign('admiddle',$admiddle);
			$this->assign('advs',$adslide);
			$this->assign('tags',$this->inforService->getTags(15));//tags
			$this->setHead($cate['name'],$cate['name'],$cate['summary']);
			$this->assign('recommend',$recommend);
			$this->assign('cate',$cate);
			$this->assign('items',$items);
            $this->setView(strtolower($request->getAction()) . '/info_more');
        }
        public function detail(HttpRequest $request) {/*资讯详情*/
			$id = $request->getParameter('id');
			$item = $this->inforService->getItem(array('id'=>$id));
			$cate = $this->inforService->getItem(array('id'=>$item['cate_id']));
			$recommend = $this->inforService->getItems(array('is_recommend'=>1,'id'=>array('!='=>$id)),array(),array(),10);

			$adtop = $this->adService->getItem(array('ad_place_key'=>"96216082fccb5cbc1011cfb520b2291f"),array(),array('addtime'=>-1));//顶部广告
			$this->assign('adtop',$adtop);

			//上一篇和下一篇
			$priv = $this->inforService->getItem(array('addtime'=>array("<"=>$item['addtime'])),array(),array('addtime'=>-1));
			$next = $this->inforService->getItem(array('addtime'=>array(">"=>$item['addtime'])),array(),array('addtime'=>1));
			$this->assign('priv',$priv);
			$this->assign('next',$next);

			//访问一次，阅读量增加一次
			$this->inforService->increase('views', 1, $id);
			$this->assign('tags',$this->inforService->getTags(15));//tags
			$this->assign('site_name',C('site_name'));
			$this->assign('cs_phone',C('cs_phone'));
			$this->setHead($item['title'],$item['tagsStr'],$item['summary']);
			$this->assign('recommend',$recommend);
			$this->assign('item',$item);
			$this->assign('cate',$cate);
            $this->setView(strtolower($request->getAction()) . '/info_detail');
        }

		public function search(HttpRequest $request)
		{
			$page = $request->getParameter('page','intval');
			$page = $page>0?$page:1;
			$tag = $request->getParameter('tag');
			if($tag=='')
			{
				location(url("/information/index/index"));
			}
			$tagsModel = Loader::model('InformationTags');
			$info_ids =  $tagsModel->getItems(array('name'=>$tag));
			$ids = array(0);
			foreach($info_ids as $item)
			{
				$ids[] = $item['information_id'];
			}
			$items = $this->inforService->getItems(array('id'=>array('$in'=>$ids)),array(),array(),array($page,$this->pagesize));
			$total = $tagsModel->count(array('name'=>$tag));
			$this->assign('items',$items);
			$this->assign('tag_label',$tag);
			$this->getPagemenu($total,0,'条资讯');

			/** ads */
			$adslide = $this->adService->getItems(array('ad_place_key'=>'1416330bee3a41053a6907bfa430626d'),array(),array(),4); //走马灯广告
			$adtop = $this->adService->getItem(array('ad_place_key'=>"2e361db4c859a5d4f7bb75becf3d066c"));//顶部广告
			$admiddle = $this->adService->getItem(array('ad_place_key'=>"a588d85d1faf09fd0612ebca0774e42c"));//中部
			$this->assign('adtop',$adtop);
			$this->assign('admiddle',$admiddle);
			$this->assign('advs',$adslide);
			$this->assign('tags',$this->inforService->getTags(15));//tags
			/** ads */
			$recommend = $this->inforService->getItems(array('is_recommend'=>1,'id'=>array('$nin'=>$ids)),array(),array('addtime'=>-1),10);
			$this->assign('recommend',$recommend);

			$this->setView(strtolower($request->getAction()) . '/info_search');
		}
	}



